package com.yoda.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.dto.RoleDto;
import com.yoda.dto.RolePageQuery;
import com.yoda.entity.SysMenu;
import com.yoda.entity.SysRole;
import com.yoda.entity.SysRoleMenu;
import com.yoda.mapper.SysMenuMapper;
import com.yoda.mapper.SysRoleMapper;
import com.yoda.mapper.SysRoleMenuMapper;
import com.yoda.result.Result;
import com.yoda.service.ISysMenuService;
import com.yoda.service.ISysRoleMenuService;
import com.yoda.service.ISysRoleService;
import com.yoda.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private ISysMenuService service;
    @Autowired
    private ISysRoleMenuService roleMenuService;


    /**
     * 角色列表-分页
     * @param pageQuery
     * @return
     */
    @Override
    public Result rolePageQuery(RolePageQuery pageQuery) {
        //开启条件查询
        LambdaQueryWrapper<SysRole> wrapper = Wrappers.lambdaQuery();
        wrapper.like(SysRole::getName, pageQuery.getName());
        //获取当前页数和每页显示多少条数据
        Integer page = Integer.valueOf(pageQuery.getPage());
        Integer size = Integer.valueOf(pageQuery.getLimit());
        IPage<SysRole> iPage = new Page<>(page, size);
        //获取到sys_role_menu表的全部数据装进集合
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(null);
        //新建一个集合
        List<RoleListVo> list = new ArrayList<>();
        List<String> collect = sysRoleMenus.stream().map(sysRoleMenu -> sysRoleMenu.getMenuId()).collect(Collectors.toList());
        //获取到role的所有数据，遍历装进list
        List<SysRole> sysRoles = sysRoleMapper.selectList(wrapper);
        RoleListVo voList = new RoleListVo();
        for (SysRole sysRole : sysRoles) {
            RoleListVo vo = new RoleListVo();
            SysRole role = sysRole;
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String format = sysRole.getCreateDate().format(fmt);
            vo.setCreate_date(format);
            BeanUtils.copyProperties(role, vo);
            list.add(vo);
        }
        //添加员工id
        voList.setMenuids(collect);
        list.add(voList);
        //新建一个vo用于数据返回
        RoleVo vo = new RoleVo();
        vo.setData(list);
        vo.setCount((int) iPage.getTotal());
        vo.setTotalPages((int) iPage.getPages());
        vo.setCurrentPage(page);
        vo.setPageSize(size);
        return Result.success(vo);
    }

    /**
     * 角色修改
     * @param dto
     * @return
     */
    @Override
    public Result updateRole(RoleListVo dto) {
        SysRole role = new SysRole();
        BeanUtils.copyProperties(dto,role);
        LambdaQueryWrapper<SysRole> wrapper = Wrappers.lambdaQuery();
        //修改员工信息
        wrapper.eq(SysRole::getId,dto.getId());
        this.update(role,wrapper);
        String id = dto.getId();
        //先将sys_role_menu的全部干掉再添加
        LambdaQueryWrapper<SysRoleMenu> roleMenu = Wrappers.lambdaQuery();
        roleMenu.eq(SysRoleMenu::getRoleId,id);
        roleMenuService.remove(roleMenu);
        //将menu添加进去
        List<String> menuids = dto.getMenuids();
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        for (String menuid : menuids) {
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenu.setRoleId(id);
            roleMenuService.save(sysRoleMenu);
        }
        return Result.success();
    }

    /**
     * 角色详情
     * @param id
     * @return
     */
    @Override
    public Result rowInfo(String id) {
        //根据id获取当前角色
        SysRole role = this.getById(id);
        RoleListVo vo = new RoleListVo();
        List<MenuList> vos = sysMenuMapper.getMenus(id, 0);
        //获取角色的权限
        for (MenuList menuList : vos) {
            List<MenuList> list = sysMenuMapper.getMenus(id, menuList.getId());
            menuList.setChildren(list);
            for (MenuList menuList1 : list) {
                List<MenuList> menus = sysMenuMapper.getMenus(id,menuList1.getId());
                menuList1.setChildren(menus);
            }
        }
        vo.setAuthList(vos);
        BeanUtils.copyProperties(role,vo);
        return Result.success(vo);
    }

    /**
     * 角色列表-全部
     * @return
     */
    @Override
    public Result getAll() {
        //直接查询所有角色
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        return Result.success(sysRoles);
    }

    /**
     * 角色添加
     * @param dto
     * @return
     */
    @Override
    public Result addRole(RoleDto dto) {

        SysRole sysRole = new SysRole();
        sysRole.setCreateDate(LocalDateTime.now());
        sysRole.setUpdateDate(LocalDateTime.now());
        BeanUtils.copyProperties(dto,sysRole);
        this.save(sysRole);
        //新增
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(sysRole.getId());
        List<String> menuids = dto.getMenuids();
        for (String menuid : menuids) {
            sysRoleMenu.setMenuId(menuid);
        }
        roleMenuService.save(sysRoleMenu);

        return Result.success();
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id) {
        //添加id判断条件进行角色删除
        LambdaQueryWrapper<SysRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRole::getId,id);
        this.remove(wrapper);
        //添加id条件删除role_menu表格
        LambdaQueryWrapper<SysRoleMenu> query = Wrappers.lambdaQuery();
        query.eq(SysRoleMenu::getRoleId,id);
        roleMenuService.remove(query);
        return Result.success();
    }

}
